package priv.lirenhe.dissertation.component.crawler_robot.url_manager.url_ini;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import priv.lirenhe.dissertation.utils.CollectionUtil;

public class SeedUrlGetter {
	private static ParamProperties notUsedSeedUrl_ParamProperties;
	private static ParamProperties usedSeedUrl_ParamProperties;
	
	public SeedUrlGetter(){
		notUsedSeedUrl_ParamProperties = new ParamProperties("not-used-seedurl.properties");
		usedSeedUrl_ParamProperties = new ParamProperties("used-seedurl.properties");
	}
	
	/**
	 * 获取一个种子url
	 * @return
	 */
	public String getOneSeedUrl(){
		String oneSeedUrlValue = null;
		String oneSeedUrlKey = null;
		Set<String> keySet = null;
		Iterator iterator = null;
		String number = null;
		String flag = null;
		Map<String,String> seedUrlInMap = notUsedSeedUrl_ParamProperties.getAllPropertyInMap();
		keySet = seedUrlInMap.keySet();
		
		System.out.println("not-used-seedurl.properties中的种子url数量 keySet.size()="+keySet.size());
		
		iterator = keySet.iterator();
		if(seedUrlInMap.size()<=0){
			return null;
		}
		//得到序号最小的未使用的key
		int loopNum = 1;
		int minNumber = 0;
		while(iterator.hasNext()){
			String key = (String) iterator.next();
			String[] keyStrs = key.split("-");
			number = keyStrs[0];
			flag = keyStrs[1];
			
			if(flag.equals("Used")){
				continue;
			}else{
				if(loopNum == 1){
					minNumber = new Integer(number);
				}else{
					if(minNumber > new Integer(number)){
						minNumber = new Integer(number);
					}
				}
			}
			loopNum++;
		}
		oneSeedUrlKey = minNumber + "-" + "NotUsed";
		
		System.out.println("降序提出种子url oneSeedUrlKey="+oneSeedUrlKey);
		
		oneSeedUrlValue = notUsedSeedUrl_ParamProperties.getPropertyByKey(oneSeedUrlKey);
		
		String oldSeedUrlKey = oneSeedUrlKey;
		String newSeedUrlKey = minNumber + "-" + "Used";
		
		notUsedSeedUrl_ParamProperties.replaceKeyOfProperty(oldSeedUrlKey,newSeedUrlKey);
		
		classifyAllSeedUrl();//对处理后的种子进行排序
		
		return oneSeedUrlValue;
	}
	
	/**
	 * 对种子ur进行分类为已使用和未使用，同时要进行升序编号
	 */
	private void classifyAllSeedUrl(){
		Map<String,String> notUsedSeedUrlInMap = notUsedSeedUrl_ParamProperties.getAllPropertyInMap();
		Map<String,String> usedSeedUrlInMap = usedSeedUrl_ParamProperties.getAllPropertyInMap();
		notUsedSeedUrl_ParamProperties.deleteAllPropertiesAndStore();
		usedSeedUrl_ParamProperties.deleteAllPropertiesAndStore();
		String seedUrlKey = null;
		String seedUrlValue = null;
		Map<String,String> notUsedMap = new LinkedHashMap<String,String>();
		
		//分类已使用和未使用的
		int newNum = 1;
		for(Map.Entry<String, String> entry:notUsedSeedUrlInMap.entrySet()){
			seedUrlKey = entry.getKey();
			seedUrlValue = entry.getValue();
			
			if(seedUrlKey.contains("NotUsed")){
				notUsedMap.put(seedUrlKey, seedUrlValue);
			}else{
				usedSeedUrlInMap.put(seedUrlKey+"-"+"New"+newNum, seedUrlValue);
			}
			newNum++;
		}
		
		//对已使用和未使用的集合重新编号
		Map<String,String> identifyNotUsedMap = new LinkedHashMap<String,String>();
		Map<String,String> identifyUsedMap = new LinkedHashMap<String,String>();
		int i = 1;
		int x = 1;
		for(Map.Entry<String, String> entry:notUsedMap.entrySet()){
			String key = entry.getKey();
			String value = entry.getValue();
			key = i + "-" + key.split("-")[1];
			identifyNotUsedMap.put(key, value);
			i++;
		}
		for(Map.Entry<String, String> entry:usedSeedUrlInMap.entrySet()){
			String key = entry.getKey();
			String value = entry.getValue();
			key = x + "-" + key.split("-")[1];
			identifyUsedMap.put(key, value);
			x++;
		}
		
		//将已使用和未使用的集合排序
		Map<String,String> sortedNotUsedMap = CollectionUtil.getSortedMap(identifyNotUsedMap);
		Map<String,String> sortedUsedMap = CollectionUtil.getSortedMap(identifyUsedMap);
		
		notUsedSeedUrl_ParamProperties.setAllPropertyAndStore(sortedNotUsedMap);
		usedSeedUrl_ParamProperties.setAllPropertyAndStore(sortedUsedMap);
	}
}



